package com.bw.day03.address

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.base.net.mvvm.BaseActivity
import com.bw.base.net.param.Address
import com.bw.base.net.param.ParamAddAddress
import com.bw.day03.R
import com.bw.day03.databinding.ActivityAddAddressBinding
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode

@Route(path =  "/app/AddAddressActivity")
class AddAddressActivity : BaseActivity<ActivityAddAddressBinding, AddressViewModel>() {
    lateinit var address: Address
    override fun initView() {
        address= Address()
        binding.addArea.setOnClickListener {
            val addressPicker = AddressPicker(this)
            addressPicker.setTitle("请选择地址")
            addressPicker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY)
            addressPicker.setOnAddressPickedListener { province, city, county ->
                binding.addArea.text = "${province.name}${city.name}${county.name}"
                address.province=province.name
                address.city=city.name
                address.district=county.name
            }
            addressPicker.show()

        }
        binding.addSave.setOnClickListener {
            val paramAddAddress= ParamAddAddress()
            paramAddAddress.detail=binding.addDetail.text.toString()
            paramAddAddress.phone=binding.addPhone.text.toString()
            paramAddAddress.realName=binding.addRealname.text.toString()
            paramAddAddress.isDefault=binding.addDefault.isChecked
            paramAddAddress.address =address
            viewModel.addAddress(paramAddAddress)
        }
    }
    override fun initData() {
        viewModel.addressSuccess.observe (this){
            if ( it.code==200){
                finish()
            }
        }

    }

    override fun getViewModel(): Class<AddressViewModel> = AddressViewModel::class.java

    override fun getLayoutId(): Int = R.layout.activity_add_address

}